package com.bw.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.SysMenu;
import com.bw.entity.SysRole;
import com.bw.mapper.SysMenuMapper;
import com.bw.mapper.SysRoleMenuMapper;
import com.bw.service.MenuService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysMenu> getAllMenu() {
        List<SysMenu> menuList = sysMenuMapper.selectList(new QueryWrapper<>());
        List<SysMenu> sysMenuList = menuList.stream().filter(sysMenu -> StrUtil.isNotBlank(sysMenu.getUrl())).collect(Collectors.toList());

        sysMenuList.forEach(sysMenu -> {
            ArrayList<String> roleList = Lists.newArrayList();
            Long menuId = sysMenu.getId();
           List<String>  stringList = sysRoleMenuMapper.selectRoleByMenuId(menuId);
           if (CollectionUtil.isNotEmpty(stringList)){
               roleList.addAll(stringList);
           }
            sysMenu.setRoleList(roleList);
        });
        return sysMenuList;
    }
}
